import {Injectable, signal} from '@angular/core';

export interface Transform {
  x: number;
  y: number;
  zoom: number;
}

@Injectable({
  providedIn: 'root'
})
export class TransformService {
  private _transform = signal<Transform>({x: 0, y: 0, zoom: 1});

  readonly transform = this._transform.asReadonly();

  setTransform(transform: Partial<Transform>) {
    this._transform.update(current => ({...current, ...transform}));
  }

  reset() {
    this._transform.set({x: 0, y: 0, zoom: 1});
  }

  zoom(factor: number) {
    this._transform.update(current => {
      const newZoom = Math.max(0.1, Math.min(3, current.zoom * factor));
      return {...current, zoom: newZoom};
    });
  }
}
